package com.dkts.oil.util;

/**
 * @author  heguanglong
 * @date 2024-06-20
 */
public class GpsConvert {
    public static final double PI = 3.14159265358979324D;
    public static final double X_PI = 3.14159265358979324D * 3000.0 / 180.0;
    private static double chinaLngMin=72.004;
    private static double chinaLngMax=137.8347;

    private static double chinaLatMin=0.8293;
    private static double chinaLatMax=55.8271;

    /**
     * BD09坐标转换GCJ02坐标
     */
    public static GpsPoint bd09ToGcj02(double bdLng, double bdLat) {
        double x = bdLng - 0.0065d, y = bdLat - 0.006d;
        double z = Math.sqrt(x * x + y * y) - 0.00002d * Math.sin(y * X_PI);
        double theta = Math.atan2(y, x) - 0.000003d * Math.cos(x * X_PI);
        double gcjLon = z * Math.cos(theta);
        double gcjLat = z * Math.sin(theta);
        return new GpsPoint(gcjLon, gcjLat);
    }



    /**
     * WGS84坐标系：即地球坐标系，国际上通用的坐标系。
     * 设备一般包含GPS芯片或者北斗芯片获取的经纬度为WGS84地理坐标系,
     */
    public static GpsPoint wgs84ToGcj02(double wgsLng, double wgsLat) {
        if (outOfChina(wgsLng, wgsLat)) {
            return new GpsPoint(wgsLng, wgsLat);
        }
        GpsPoint d = delta(wgsLng, wgsLat);
        return new GpsPoint(d.lon + wgsLng, d.lat + wgsLat);
    }


    public static GpsPoint gcjDecrypt(double gcjLng, double gcjLat) {
        if (outOfChina(gcjLng, gcjLat)) {
            return new GpsPoint(gcjLng, gcjLat);
        }
        GpsPoint d = delta(gcjLng, gcjLat);
        return new GpsPoint(gcjLng - d.lon, gcjLat - d.lat);
    }
    /**
     * GCJ-02(火星坐标系)转WGS84
     * @param gcjLon 火星坐标系经度
     * @param gcjLat 火星坐标系纬度
     * @return WGS84坐标数组
     */
    public static GpsPoint gcj02ToWgs84(double gcjLon, double gcjLat) {
        double[] wgs84 = transform(gcjLon, gcjLat);
        return new GpsPoint(gcjLon * 2 - wgs84[0], gcjLat * 2 - wgs84[1]);
    }

    /**
     * WGS84坐标转换辅助函数
     * @param lon 经度
     * @param lat 纬度
     * @return 偏移后的WGS84坐标
     */
    public static double[] transform(double lon, double lat) {
        if (outOfChina(lon, lat)) {
            return new double[] {lon, lat};
        }
        double dLat = transformLat(lon - 105.0, lat - 35.0);
        double dLon = transformLon(lon - 105.0, lat - 35.0);
        double radLat = lat / 180.0 * PI;
        double magic = Math.sin(radLat);
        magic = 1 - 0.00669342162296594323 * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((6335552.7170004264 / (magic * sqrtMagic)) * PI);
        dLon = (dLon * 180.0) / ((6378245.0 / sqrtMagic) * Math.cos(radLat) * PI);
        return new double[] {lon + dLon, lat + dLat};
    }
    /**
     * GCJ02坐标系：即火星坐标系，
     * 是由中国国家测绘局制订的地理信息系统的坐标系统。由WGS84坐标系经加密后的坐标系。
     */
    public static GpsPoint gcj02Bd09(double gcjLng, double gcjLat) {
        double x = gcjLat;
        double y = gcjLng;
        double z = Math.sqrt(x * x + y * y) + 0.00002D * Math.sin(y * X_PI);
        double theta = Math.atan2(y, x) + 0.000003D * Math.cos(x * X_PI);
        double bdLat = z * Math.sin(theta) + 0.006D;
        double bdLon = z * Math.cos(theta) + 0.0065D;
        return new GpsPoint(bdLat, bdLon);
    }
    /**
     * 墨卡托投影
     * 墨卡托(Mercator)投影，又名“等角正轴圆柱投影”，
     * 荷兰地图学家墨卡托（Mercator）在1569年拟定，
     * 假设地球被围在一个中空的圆柱 里，其赤道与圆柱相接触，
     * 然后再假想地球中心有一盏灯，把球面上的图形投影到圆柱体上，
     * 再把圆柱体展开，这就是一幅标准纬线为零度（即赤道）的“墨卡托投 影”绘制出的世界地图。
     */
    public static GpsPoint wGs84Mercator(double wgsLng, double wgsLat) {
        double x = wgsLng * 20037508.34D / 180.0;
        double y = Math.log(Math.tan((90.0 + wgsLat) * PI / 360.0)) / (PI / 180.);
        y = y * 20037508.34D / 180.0;
        return new GpsPoint(x, y);
    }

    public static GpsPoint mercatorWgs84(double mercatorLng, double mercatorLat) {
        double x = mercatorLng / 20037508.34d * 180.;
        double y = mercatorLat / 20037508.34d * 180.;
        y = 180 / PI * (2 * Math.atan(Math.exp(y * PI / 180.0)) - PI / 2);
        return new GpsPoint(x, y);
    }


    public static double distance(double lngA, double latA, double lngB, double latB) {
        int earthR = 6371000;
        double x = Math.cos(latA * PI / 180.) * Math.cos(latB * PI / 180.) * Math.cos((lngA - lngB) * PI / 180);
        double y = Math.sin(latA * PI / 180.) * Math.sin(latB * PI / 180.);
        double s = x + y;
        if (s > 1) {
            s = 1;
        }
        if (s < -1) {
            s = -1;
        }
        double alpha = Math.acos(s);
        double distance = alpha * earthR;
        return distance;
    }


    public static double distance(GpsPoint p1, GpsPoint p2) {
        return distance(p1.lon, p1.lat, p2.lon, p2.lat);
    }



    public static boolean outOfChina(double lng, double lat) {
        if (lng <chinaLngMin  || lng > chinaLngMax) {
            return true;
        }
        if (lat < chinaLatMin || lat > chinaLatMax) {
            return true;
        }
        return false;
    }

    private static double transformLat(double x, double y) {
        double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * PI) + 40.0 * Math.sin(y / 3.0 * PI)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * PI) + 320 * Math.sin(y * PI / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    private static double transformLon(double x, double y) {
        double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * PI) + 40.0 * Math.sin(x / 3.0 * PI)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * PI) + 300.0 * Math.sin(x / 30.0 * PI)) * 2.0 / 3.0;
        return ret;
    }

    private static GpsPoint delta(double lng, double lat) {
        double a = 6378245.0;
        double ee = 0.00669342162296594323;
        double dLat = transformLat(lng - 105.0, lat - 35.0);
        double dLon = transformLon(lng - 105.0, lat - 35.0);
        double radLat = lat / 180.0 * PI;
        double magic = Math.sin(radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * PI);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * PI);
        return new GpsPoint(dLon, dLat);
    }

    public static class GpsPoint {
        public double lon;
        public double lat;
        public GpsPoint() {
        }
        public GpsPoint(double lon, double lat) {
            this.lat = lat;
            this.lon = lon;
        }
    }
}
